//
//  ReportView.m
//  ZITHER
//
//  Created by weixin on 2018/12/6.
//  Copyright © 2018 weixn. All rights reserved.
//

#import "ReportView.h"
#import "ReportView_WX.h"

@interface ReportView()
/** 底部取消Button, 默认标题为"取消" */
//@property (nonatomic, strong) UIView *view1;//举报列表

@property (nonatomic, strong) UIView *dimBackgroundView;/**< 半透明黑色背景 */
@property (nonatomic, strong) UIWindow *window;
@property (nonatomic,strong)ReportView_WX * tbv;
@end

@implementation ReportView

#pragma mark - public method


- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self commonInit];
    }
    return self;
}

- (void)commonInit
{
    
    self.frame = CGRectMake(0, 0, kScreenW, kScreenH);
    
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hide) name:ZY_HideNotification object:nil];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)show
{
    [self addToKeyWindow];
    [self showAnimationWithCompletion:nil];
}

- (void)hide
{
    [self hideAnimationWithCompletion:^(BOOL finished) {
        [self removeFromKeyWindow];
    }];
}

#pragma mark - private method

- (void)addToKeyWindow
{
    if (!self.superview) {
        UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
        [keyWindow addSubview:self];
        
        [self addSubview:self.dimBackgroundView];
        [self addSubview:self.tbv];
        [self addSubview:self.cancelButton];
    }
}

- (void)removeFromKeyWindow
{
    if (self.superview) {
        [self removeFromSuperview];
    }
}

- (void)showAnimationWithCompletion:(void (^)(BOOL finished))completion
{
    [UIView animateWithDuration:0.3 animations:^{
        self.dimBackgroundView.alpha = 0.3;
    } completion:completion];
}

- (void)hideAnimationWithCompletion:(void (^)(BOOL finished))completion
{
    [UIView animateWithDuration:0.3 animations:^{
        self.dimBackgroundView.alpha = 0;
    } completion:completion];
}

#pragma mark - getter
- (UIView *)dimBackgroundView
{
    if (!_dimBackgroundView) {
        _dimBackgroundView = [[UIView alloc] init];
        _dimBackgroundView.frame = CGRectMake(0, 0, kScreenW, kScreenH);
        _dimBackgroundView.backgroundColor = [UIColor blackColor];
        _dimBackgroundView.alpha = 0;
        
        // 添加手势监听
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
        [_dimBackgroundView addGestureRecognizer:tap];
    }
    return _dimBackgroundView;
}

//-(UIView *)view1{
//    if (!_view1) {
//        _view1 = [[UIView alloc] init];
//        _view1.frame = CGRectMake(10, kScreenH-300, kScreenW-20,300);
//        _view1.backgroundColor = [UIColor whiteColor];
//        [_view1 setCornerRadiusWithRadius:8 isMasks:YES];
//        // 添加手势监听
//        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
//        [_view1 addGestureRecognizer:tap];
//    }
//    return _view1;
//}

-(ReportView_WX *)tbv{
    if (!_tbv) {
        CGFloat H = (kScreenW-20)/355*243;
        _tbv = [[ReportView_WX alloc]initializeFrame:CGRectMake(10, kScreenH-H-80, kScreenW-20 , H) isMj:0 isEstimatedRowHeight:YES addRegisterNib:@[@"ReportView_WX_Cell"]];
        _tbv.backgroundColor = [UIColor whiteColor];
        [_tbv setCornerRadiusWithRadius:8 isMasks:YES];
        // 添加手势监听
//        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
//        [_tbv addGestureRecognizer:tap];
    }
    return _tbv;
}

-(BaseBtn *)cancelButton{
    if (!_cancelButton) {
        _cancelButton = [[BaseBtn alloc]initializeFrame:CGRectMake(10,kScreenH-55, kScreenW-20, 45) NormalText:@"确定" NormalTextColor:[UIColor colorWithHex:@"333333"] SeleText:nil SeleTextColor:nil font:17 isBold:NO];
        [_cancelButton setCornerRadiusWithRadius:8 isMasks:YES];
        _cancelButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:17];
        @weakify(self);
        [[_cancelButton rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
            
            NSDictionary *dic = self.tbv.arr[self.tbv.index];
            NSString *str = @"";
            
            switch (self.state) {
                case ReportViewStatePgc:
                    str = @"PGC";
                    break;
                case ReportViewStateUgc:
                    str = @"UGC";
                    break;
                case ReportViewStateComent:
                    str = @"COMENT";
                    break;
                default:
                    break;
            }
            
            [[serviceDefualt loadReportRype:str sourceId:self.typeId category:dic[@"value"]  ReponseObjctClass:nil]subscribeNext:^(ResponseData * x) {
                if (x.success) {
                    [Tools showTips:@"举报成功"];
                }else{
                     [Tools showTips:x.errorInfo];
                }
            }];
            
            [self hide];
        }];
    }
    return _cancelButton;
}

@end
